package com.primaryAlgorithm;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-29 14:00:38
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-29     张李鑫                     1.0         1.0 Version
 */
public class MinStack {

    private List<Integer> arr = new ArrayList<Integer>();

    private Integer min = null;

    public MinStack() {

    }

    public void push(int val) {
        arr.add(val);
        min = min == null ? val : (val > min ? min : val);
    }

    public void pop() {
        if (arr.get(arr.size() - 1).equals(min)) {
            Integer findMin = null;
            for (int i = 0; i < arr.size() - 1; i++) {
                findMin = findMin == null ? arr.get(i) : findMin > arr.get(i) ? arr.get(i) : findMin;
            }
            min = findMin;

        }
        arr.remove(arr.size() - 1);
    }

    public int top() {
        return arr.get(arr.size() - 1);
    }

    public int getMin() {
        return min;
    }
}
